package com.ruoyi.web.controller.system;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Gantt;
import com.ruoyi.system.domain.Plan;
import com.ruoyi.system.domain.SysTreeDictData;
import com.ruoyi.system.service.IPlanService;
import com.ruoyi.system.service.ISysTreeDictDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.service.IProjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工程管理Controller
 * 
 * @author genghz
 * @date 2021-12-18
 */
@RestController
@RequestMapping("/project/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    @Autowired
    private IPlanService planService ;

    /**
     * 查询工程管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出工程管理列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:export')")
    @Log(title = "工程管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        Plan plan = planService.selectPlanByPlanId(project.getPlanId());
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.getNowDate());
        return util.exportExcel(list, plan.getPlanName() + "_" + date);
    }

    /**
     * 获取工程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId)
    {
        return AjaxResult.success(projectService.selectProjectByTaskId(taskId));
    }

    /**
     * 新增工程管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "工程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project project)
    {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改工程管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "工程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project)
    {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除工程管理
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "工程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(projectService.deleteProjectByTaskIds(taskIds));
    }


    @GetMapping(value = "/getDistinctSystem/{planId}")
    public TableDataInfo getDistinctSystem(@PathVariable("planId") Long planId)
    {
        List<String> list = projectService.getDistinctSystem(planId);
        return getDataTable(list);
    }

    @GetMapping(value = "/getDistinctSubSystem")
    public TableDataInfo getDistinctSubSystem(String sysLel1)
    {
        List<String> list = projectService.getDistinctSubSystem(sysLel1);
        return getDataTable(list);
    }

    @GetMapping(value = "/getDistinctDept")
    public TableDataInfo getDistinctDept()
    {
        List<String> list = projectService.getDistinctDept();
        return getDataTable(list);
    }

    @GetMapping(value = "/getDistinctTask/{planId}")
    public TableDataInfo getDistinctTask(@PathVariable("planId") Long planId)
    {
        List<Project> list = projectService.getDistinctTask(planId);
        return getDataTable(list);
    }

    @Autowired
    private ISysTreeDictDataService sysTreeDictDataService;


    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.importTemplateExcel("计划编排");
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('project:project:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Long planId) throws Exception
    {
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        List<Project> projectList = util.importExcel(file.getInputStream());
        String message = projectService.importTask(projectList, planId);
        return AjaxResult.success(message);
    }

    @GetMapping("/gantt/{planId}")
    public AjaxResult gantt(@PathVariable("planId") Long planId) {
        Gantt gantt = projectService.gantt(planId) ;
        return AjaxResult.success(gantt);
    }

}
